/* eslint-disable comma-dangle */
const path = require('path')
const ScriptPlugin = require('./build/script-plugin')
const filterRemove = require('./build/filter-remove')
const CompileEventsPlugin = require('./build/compile-events-plugin')
const outputPath = 'E:/github/cqlql.github.io/note'
const conf = {
  outputDir: outputPath,
  publicPath: '',
  productionSourceMap: false,
  css: {
    extract: false,
    sourceMap: process.env.NODE_ENV !== 'production'
  },
  devServer: {
    contentBase: outputPath,
    historyApiFallback: false, // HTML5 History 模式
    // proxy: {
    //   '/8186': {
    //     target: 'http://192.168.1.162:8186',
    //     secure: true,
    //     pathRewrite: {
    //       '^/8186': ''
    //     }
    //   }
    // }
  },
  configureWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      // 为生产环境修改配置...

      config.externals = {
        ...config.externals,
        // 'vue': 'Vue',
      }
      config.plugins = [
        ...config.plugins,
        // new ScriptPlugin([
        //   'https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js'
        // ]),
        // 编译事件
        new CompileEventsPlugin({
          compile () {
            // 编译前删除
            filterRemove(outputPath, /\.js$/)
          },
          done () {
            // 编译后
          }
        })
      ]
      // js拆分
      config.optimization.splitChunks = {}
    } else {
      // 为开发环境修改配置...
    }
  },
  chainWebpack: config => {
    // 取消 eslint
    const eslint = config.module.rule('eslint')
    eslint.uses.clear()

    // url-loader css @
    // config.resolve
    //   .alias
    //   .set('./@', path.resolve(__dirname, 'src'))

    // 修改环境变量
    // config.plugin('define')
    //   .tap(options => {
    //     options[0]['process.env'].temp_data_handle = 'true'
    //     return options
    //   })

    // if (process.env.NODE_ENV === 'production') {
    //   config
    //     .externals({
    //       ...config.get('externals'),
    //       vue: 'Vue'
    //     })

    //   // chainWebpack 方式新增插件
    //   config.plugin('addScript')
    //     .use(ScriptPlugin, [
    //       'https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js'
    //     ])
    // }
  }
}
module.exports = conf
